package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {


    @Reference
    private AddressService addressService;

    /**
     * 查询所有分院的信息
     * @return
     */
    @GetMapping("/findAll")
    public Result addressFindAll(){
        try {
            List<Address> addressList = addressService.findAll();
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS,addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    //添加所有分院信息
    @PostMapping("/submit")
    public Result addressSubmit(@RequestBody Address address){
        try {
            Boolean result=addressService.submit(address);
            return new Result(true,MessageConstant.ADD_ADDRESS_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    //删除
    @DeleteMapping("/{id}")
    public Result addressDelete(@PathVariable Integer id){
        try {
            Boolean result=addressService.delete(id);
            return new Result(true,"删除成功",result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

    //单个查询
    @GetMapping("/{id}")
    public Result addressFindById(@PathVariable Integer id){
        try {
            Address result=addressService.findById(id);
            return new Result(true,MessageConstant.QUERY_ADDRESS_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }
    //编辑
    @PutMapping("update")
    public Result addressUpdate(@RequestBody Address address){
        try {
            boolean result = addressService.addressUpdate(address);
            return new Result(true,MessageConstant.EDIT_ADDRESS_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }

    //是否启用
    @PutMapping("enableupdate/{enable}/{id}")
    public  Result enableUpdate(@PathVariable String enable,
                                @PathVariable Integer id){
        try {
            boolean result = addressService.enableUpdate(enable,id);
            return new Result(true,"修改成功",result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }
}
